//导入request.js请求工具
import request from '@/utils/request.js'

// 管理员登录接口
export const adminLoginService = (loginData) => {
  return request.post('/adminUser/login', loginData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 获取管理员个人信息
export const getAdminInfoService = () => {
  return request.get('/adminUser/getAdminInfo');
}

// ==================== 用户审核管理接口 ====================

// 获取待审核用户列表（学生和教师）
export const getPendingUsersService = (userType = '') => {
  return request.get('/admin/user/pending', {
    params: {
      userType: userType || undefined
    }
  });
}

// 审核用户（通过或驳回）
export const auditUserService = (auditData) => {
  return request.post('/admin/user/audit', auditData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 注销用户
export const deactivateUserService = (userId, userType) => {
  return request.post('/admin/user/deactivate', null, {
    params: {
      userId: userId,
      userType: userType
    }
  });
}

// 用户搜索
export const searchUsersService = (searchData) => {
  return request.post('/admin/user/search', searchData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 获取用户详情
export const getUserDetailService = (loginId, userType) => {
  return request.get('/admin/user/detail', {
    params: {
      loginId,
      userType
    }
  });
}

// 更新用户信息
export const updateUserService = (userData) => {
  return request.post('/admin/user/update', userData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 获取驳回记录
export const getRejectedUsersService = () => {
  return request.get('/admin/user/rejected');
}

// 获取注销记录
export const getDeactivatedUsersService = () => {
  return request.get('/admin/user/deactivated');
}

// 启用用户
export const activateUserService = (userId, userType) => {
  return request.post('/admin/user/activate', null, {
    params: {
      userId: userId,
      userType: userType
    }
  });
}

// 获取教师库
export const getTeachersService = (page = 1, size = 10, keyword = '') => {
  return request.get('/admin/user/teachers', {
    params: {
      page,
      size,
      keyword: keyword || undefined
    }
  });
}

// 获取学生库
export const getStudentsService = (page = 1, size = 10, keyword = '') => {
  return request.get('/admin/user/students', {
    params: {
      page,
      size,
      keyword: keyword || undefined
    }
  });
}

// ==================== 用户统计接口 ====================

// 获取用户统计数据
export const getUserStatisticsService = () => {
  return request.get('/admin/user/statistics');
}

// 获取用户增减情况
export const getUserTrendsService = (period = 'month') => {
  return request.get('/admin/user/trends', {
    params: {
      period
    }
  });
}

// 获取每日用户统计数据
export const getDailyUserStatisticsService = (days = 7) => {
  return request.get('/admin/user/daily-statistics', {
    params: {
      days
    }
  });
}

// 获取所有可用技能列表
export const getSkillsListService = () => {
  return request.get('/skills/list');
}

// ==================== 主页编辑接口 ====================

// 获取主页内容
export const getHomepageContentService = () => {
  return request.get('/adminUser/homepageContent');
}

// 更新主页内容
export const updateHomepageContentService = (contentData) => {
  return request.put('/adminUser/homepageContent', contentData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// ==================== 轮播图管理接口 ====================

// 获取轮播图列表
export const getCarouselImagesService = () => {
  return request.get('/adminUser/carousel');
}

// 更新轮播图
export const updateCarouselImageService = (position, imageData) => {
  return request.put(`/adminUser/carousel/${position}`, imageData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 上传轮播图图片
export const uploadCarouselImageService = (file) => {
  const formData = new FormData();
  formData.append('file', file);
  return request.post('/adminUser/carousel/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

// ==================== 系统通知管理接口 ====================

// 获取系统通知列表
export const getSystemNotificationsService = (params = {}) => {
  // 设置默认参数
  const defaultParams = {
    page: 1,
    size: 10,
    ...params
  };

  return request.get('/adminUser/system-notifications', {
    params: defaultParams
  });
}

// 获取系统通知详情
export const getSystemNotificationService = (notificationId) => {
  return request.get(`/adminUser/system-notifications/${notificationId}`);
}

// 创建/发布系统通知
export const createSystemNotificationService = (notificationData) => {
  return request.post('/adminUser/system-notifications', notificationData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 更新系统通知
export const updateSystemNotificationService = (notificationId, notificationData) => {
  return request.put(`/adminUser/system-notifications/${notificationId}`, notificationData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 删除系统通知
export const deleteSystemNotificationService = (notificationId) => {
  return request.delete(`/adminUser/system-notifications/${notificationId}`);
}

// 上传通知图片
export const uploadNotificationImageService = (file) => {
  const formData = new FormData();
  formData.append('file', file);
  return request.post('/adminUser/system-notifications/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}



// ==================== 新闻管理接口 ====================

// 获取新闻列表
export const getNewsListService = (params = {}) => {
  // 设置默认参数
  const defaultParams = {
    page: 1,
    size: 10,
    ...params
  };

  return request.get('/adminUser/news', {
    params: defaultParams
  });
}

// 获取新闻详情
export const getNewsDetailService = (newsId) => {
  return request.get(`/adminUser/news/${newsId}`);
}

// 创建新闻
export const createNewsService = (newsData) => {
  return request.post('/adminUser/news', newsData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 更新新闻
export const updateNewsService = (newsId, newsData) => {
  return request.put(`/adminUser/news/${newsId}`, newsData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 删除新闻
export const deleteNewsService = (newsId) => {
  return request.delete(`/adminUser/news/${newsId}`);
}

// 上传新闻图片
export const uploadNewsImageService = (file) => {
  const formData = new FormData();
  formData.append('file', file);
  return request.post('/adminUser/news/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

// 兼容旧接口名称
export const getAllNewsService = getNewsListService;
export const publishNewsService = createNewsService;

// ==================== 赛事管理接口 ====================

// 获取赛事列表
export const getEventListService = (params = {}) => {
  // 设置默认参数
  const defaultParams = {
    page: 1,
    size: 10,
    ...params
  };

  return request.get('/adminUser/events', {
    params: defaultParams
  });
}

// 获取赛事详情
export const getEventDetailService = (eventId) => {
  return request.get(`/adminUser/events/${eventId}`);
}

// 创建赛事
export const createEventService = (eventData) => {
  return request.post('/adminUser/events', eventData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 更新赛事
export const updateEventService = (eventId, eventData) => {
  return request.put(`/adminUser/events/${eventId}`, eventData, {
    headers: {
      'Content-Type': 'application/json'
    }
  });
}

// 删除赛事
export const deleteEventService = (eventId) => {
  return request.delete(`/adminUser/events/${eventId}`);
}

// 上传赛事图片
export const uploadEventImageService = (file) => {
  const formData = new FormData();
  formData.append('file', file);
  return request.post('/adminUser/events/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

// ==================== 队伍管理接口 ====================

// 获取所有队伍列表
export const getTeamListService = () => {
  return request.get('/adminTeam/list');
}

// 查看队伍详细信息
export const getTeamDetailService = (teamId) => {
  return request.get(`/adminTeam/detail/${teamId}`);
}

// 搜索队伍
export const searchTeamsService = (keyword) => {
  return request.get('/adminTeam/search', {
    params: {
      keyword: keyword
    }
  });
}

// ==================== 用户数据导出接口 ====================

// 导出待审核用户数据
export const exportPendingUsersService = () => {
  return request.get('/admin/user/export/pending', {
    responseType: 'blob',
    timeout: 300000, // 5分钟超时
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  });
}

// 导出被驳回用户数据
export const exportRejectedUsersService = () => {
  return request.get('/admin/user/export/rejected', {
    responseType: 'blob',
    timeout: 300000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  });
}

// 导出被注销用户数据
export const exportDeactivatedUsersService = () => {
  return request.get('/admin/user/export/deactivated', {
    responseType: 'blob',
    timeout: 300000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  });
}

// 导出全部用户数据
export const exportAllUsersService = () => {
  return request.get('/admin/user/export/all', {
    responseType: 'blob',
    timeout: 300000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  });
}
